package com.example.tablemainpage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

public class MainFuPage extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


    private VideoView videoFuView;
    private Button btnFuPause, btnFastFuForward, btnFuRewind;
    private ImageView imageFuView;
    private TextView textFuView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fu_page);

        int ui = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        getWindow().getDecorView().setSystemUiVisibility(ui);
        getSupportActionBar().hide();

        videoFuView = findViewById(R.id.videoFuView);
        imageFuView = findViewById(R.id.imgPreFuview);
        btnFuPause = findViewById(R.id.btnFuPause);
        btnFastFuForward = findViewById(R.id.btnFastFuForward);
        btnFuRewind = findViewById(R.id.btnFuRewind);
        textFuView = findViewById(R.id.textFuView);

        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
        videoFuView.setVideoURI(uri);

        SharedPreferences sp = getSharedPreferences("video", MODE_PRIVATE);
        sp.registerOnSharedPreferenceChangeListener(this);
        SharedPreferences.Editor editor = sp.edit();
        if (sp.getInt("time", 0) != 0) {
            textFuView.setText("当前播放到第" + sp.getInt("time", 0) / 1000 + "秒");
        }

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(this, uri);
            Bitmap bitmap = retriever.getFrameAtTime(sp.getInt("time", 0));
            if (bitmap != null) {
                imageFuView.setImageBitmap(bitmap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        btnFuPause.setOnClickListener(v -> {
            imageFuView.setVisibility(View.GONE);
            if (videoFuView.isPlaying()) {
                videoFuView.pause();
                editor.putInt("time", videoFuView.getCurrentPosition());
                editor.putBoolean("ispaly", videoFuView.isPlaying());
                System.out.println(sp.getBoolean("isplay", false));
                editor.apply();
                btnFuPause.setText("播放");
            } else {
                editor.putBoolean("ispaly", !videoFuView.isPlaying());
                editor.apply();
                videoFuView.start();
                btnFuPause.setText("暂停");
            }
        });
        btnFastFuForward.setOnClickListener(v -> {
            int currentPosition = videoFuView.getCurrentPosition();
            editor.putInt("nowtime", currentPosition + 1000);
            editor.apply();
            videoFuView.seekTo(currentPosition + 1000); // 快进5秒
        });
        btnFastFuForward.setOnClickListener(v -> {
            int currentPosition = videoFuView.getCurrentPosition();
            editor.putInt("nowtime", currentPosition - 1000);
            editor.apply();
            videoFuView.seekTo(currentPosition - 1000); // 后退5秒
        });

        videoFuView.setOnPreparedListener(mediaPlayer -> {
            videoFuView.seekTo(sp.getInt("time", 0));
            btnFuPause.setText("播放");
        });

        videoFuView.setOnCompletionListener(mediaPlayer -> {
            videoFuView.seekTo(0);
            videoFuView.pause();
        });
        videoFuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (videoFuView.isPlaying()) {
                    videoFuView.pause();
                    editor.putBoolean("ispaly", videoFuView.isPlaying());
                    editor.apply();
                } else {
                    editor.putBoolean("ispaly", !videoFuView.isPlaying());
                    editor.apply();
                    videoFuView.start();
                }
            }
        });

        imageFuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageFuView.setVisibility(View.GONE);
                editor.putBoolean("ispaly", !videoFuView.isPlaying());
                editor.apply();
                videoFuView.start();
            }
        });


    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sp, String s) {
        switch (s) {
            case "ispaly":
                if (sp.getBoolean("ispaly", false)) {
                    imageFuView.setVisibility(View.GONE);
                    videoFuView.start();
                    btnFuPause.setText("暂停");
                } else {
                    videoFuView.pause();
                    btnFuPause.setText("播放");
                }
                break;
            case "time":
                break;
            case "nowtime":
                videoFuView.seekTo(sp.getInt("nowtime", 0));
                break;
        }
    }
}